package com.innjia.base.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 类DateUtils的功能描述:
 * 日期处理
 *
 * @auther innjia
 * @date 2017-08-25 16:12:36
 */
public class DateUtils {
    /**
     * 时间格式(yyyy-MM-dd)
     */
    public final static String DATE_PATTERN = "yyyy-MM-dd";

    /**
     * 时间格式(yyyy-MM-dd HH:mm:ss)
     */
    public final static String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";

    private static final ThreadLocal<SimpleDateFormat> DATE_FORMAT_THREAD_LOCAL = new ThreadLocal<>();

    public static String format(Date date) {
        return format(date, DATE_PATTERN);
    }

    public static String format(Date date, String pattern) {
        if (date != null) {
            SimpleDateFormat df = DATE_FORMAT_THREAD_LOCAL.get();
            if (null == df) {
                df = new SimpleDateFormat();
                DATE_FORMAT_THREAD_LOCAL.set(df);
            }
            df.applyPattern(pattern);
            return df.format(date);
        }
        return null;
    }
    
    /**
     * 计算合同有效截止时间，当前日期-月数month-天数1
     * @param date
     * @param month
     * @return
     * Date
     */
    public static Date contractValidityEndTime(Date date, int month) {
    	if(month <= 0) {
    		return date;
    	}
    	if(date != null) {
    		Calendar cal = Calendar.getInstance();
    		cal.setTime(date);
    		cal.add(Calendar.MONTH, month);
    		cal.add(Calendar.DAY_OF_MONTH, -1);
    		return cal.getTime();
    	}
    	return null;
    }
}
